package se.webbzon.boltzmann.minigame.botany;

import java.awt.Image;

import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxContainer;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.oschi01.resourceset.ResourceSet;

public abstract class SandBoxCell extends DefaultSandBoxItem implements SandBoxContainer {
	
	// The sand box attached to this cell
	private SandBox box;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public SandBoxCell() {
		setInstance(new Instance());
		setDraggable(true);
		setDroppable(true);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public boolean isEmpty() {
		return true;
	}

	@Override public SandBoxItem getContent() {
		return null;
	}

	@Override public void setContent(SandBoxItem item) {
		if (consumeResource(item) && box != null)
			box.removeItem(item);
	}
	
	@Override public void onAttach(SandBox box) {
		super.onAttach(box);
		this.box = box;
	}

	@Override public void onDetach(SandBox box) {
		this.box = null;
		super.onDetach(box);
	}
	
	/*============================================================
	Protected Methods
	============================================================*/
	
	/** Returns the current sand box of this cell. **/
	protected SandBox getSandBox() {
		return box;
	}
	
	/** Consumes the provided resource to regenerate. If the resource
	 * was consumed then true must be returned. **/
	protected abstract boolean consumeResource(SandBoxItem item);
	
	/** Returns the image set of the cell. **/
	protected abstract ResourceSet<Image> getImage();
	
	/** Returns true if this cell is health enough to live on. **/
	protected abstract boolean isHealthy();
	
	/** Returns true if this cell is health enough to reproduce. **/
	protected abstract boolean canSplit();
	
	/** Returns true if this cell is health enough to react. **/
	protected abstract boolean canReact();
	
	/** Performs an idle cell consumption. **/
	protected abstract void onConsume();
	
	/** Performs a cell reaction. **/
	protected abstract void onReact();
	
	/** Performs a cell split. **/
	protected abstract void onSplit();
	
	@Override protected AbstractCell getInstance() {
		return (AbstractCell) super.getInstance();
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class Instance extends AbstractCell {

		@Override protected ResourceSet<Image> getImage() {
			return SandBoxCell.this.getImage();
		}

		@Override protected boolean isHealthy() {
			return SandBoxCell.this.isHealthy();
		}
		
		@Override protected boolean canSplit() {
			return SandBoxCell.this.canSplit();
		}

		@Override protected void onConsume() {
			SandBoxCell.this.onConsume();
		}

		@Override protected boolean canReact() {
			return SandBoxCell.this.canReact();
		}

		@Override protected void onReact() {
			SandBoxCell.this.onReact();
		}

		@Override protected void onSplit() {
			SandBoxCell.this.onSplit();
		}
		
	}

}
